package com.github.llyb120.yaomq.server;

import cn.hutool.core.thread.ThreadUtil;
import lombok.val;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Iterator;
import java.util.Map;

import static com.github.llyb120.yaomq.server.MQService.lock;
import static com.github.llyb120.yaomq.server.MQService.messageBufferMap;

/**
 * @Author: Administrator
 * @Date: 2020/9/14 17:44
 */
@Component
public class MapCleaner {

    @PostConstruct
    public void init(){
        //启动回收线程
        ThreadUtil.execute(() -> {
            while(true){
                ThreadUtil.sleep(30000);
                clean();
            }
        });
    }

    public void clean(){
        long now = System.currentTimeMillis();
        messageBufferMap.entrySet().removeIf(element -> element.getValue().getExpr() < now);
    }
}
